package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AddDishToSetmealNotAllowedException;
import com.sky.exception.BaseException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询方法
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult setmealQueryAllByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> date = setmealMapper.setmealQueryAllByPage(setmealPageQueryDTO);

        //设置返回结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(date.getTotal());
        pageResult.setRecords(date.getResult());
        return pageResult;
    }

    /**
     * 新增方法
     * @param setmealDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    //TODO 把菜品与套餐的关联写在setmeal_dishs表中
    @Override
    public boolean insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增了套餐
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        //新增了套餐与分类之间的依赖首先判断这个依赖里的菜品是否都启用
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //菜品没启用
            //TODO 写到修改起售套餐中
            if (dishMapper.getDishById(setmealDish.getDishId()).getStatus() == StatusConstant.DISABLE){
                //菜品禁用中，禁止添加到起售套餐
                throw new AddDishToSetmealNotAllowedException(MessageConstant.SETMEAL_STATUS_ERROR);
            }
            setmealDish.setSetmealId(setmealId);
            boolean b = setmealDishMapper.insert(setmealDish);
            if (!b) {
                throw new BaseException(MessageConstant.SETMEAL_ADD_ERROR);
            }
            //TODO 写到修改起售套餐中
            /*if (dishMapper.getDishById(setmealDish.getDishId()) != null){
                //菜品禁用中，禁止添加到起售套餐
                throw new AddDishToSetmealNotAllowedException();
            }*/
        }

        return true;
    }


    /**
     * 套餐修改
     * @param setmealDTO
     * @return
     */
    //TODO 记得在修改套餐时加上要修改的宝贝菜品
    @Transactional
    @Override
    public boolean updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);
        //删除原有的菜品中间表
        boolean b = setmealDishMapper.deleteById(setmeal.getId());
        Long setmealId = setmeal.getId();
        //新增了套餐与分类之间的依赖首先判断这个依赖里的菜品是否都启用
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //菜品没启用
            //TODO 写到修改起售套餐中
            if (dishMapper.getDishById(setmealDish.getDishId()).getStatus() == StatusConstant.DISABLE){
                //菜品禁用中，禁止添加到起售套餐
                //throw new AddDishToSetmealNotAllowedException(MessageConstant.SETMEAL_STATUS_ERROR);
            }
            setmealDish.setSetmealId(setmealId);
            boolean b1 = setmealDishMapper.insert(setmealDish);
            if (!b) {
                throw new BaseException(MessageConstant.SETMEAL_ADD_ERROR);
            }
            //TODO 写到修改起售套餐中
            /*if (dishMapper.getDishById(setmealDish.getDishId()) != null){
                //菜品禁用中，禁止添加到起售套餐
                throw new AddDishToSetmealNotAllowedException();
            }*/
        }

        return true;
    }

    /**
     * 修改前的回显
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> dishs = setmealDishMapper.getDishById(id);
        setmealVO.setSetmealDishes(dishs);
        return setmealVO;
    }

    /**
     * 启用禁用方法
     * @param status
     * @param id
     * @return
     */
    @Override
    public boolean startAndStopSetmeal(Integer status, Long id) {
        if(status == StatusConstant.ENABLE){
            List<Dish> dishList = dishMapper.getDishBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
        return true;
    }

    /**
     * 套餐删除
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public boolean deleteSetmeal(Long[] ids) {

        boolean b = setmealMapper.delete(ids);
        for (Long id : ids) {
            if (setmealMapper.selectStatus(id) != StatusConstant.ENABLE){
                //套餐使用中，禁止删除套餐
                throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
            }
            boolean b1 = setmealDishMapper.deleteById(id);
            if (!b1) {
                throw new BaseException("删除套餐中间表错误");
            }
        }
        return b;
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
